package androidApplication;


import java.io.IOException;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import javax.net.ssl.SSLSocket;

import security.Encryption;

import connectionService.ConnectionServiceAndroidClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import entities.Device;
import entities.DeviceType;
import entities.User;
import fileService.FileServiceHost;

public class Commons extends Activity implements Serializable,Runnable{
	private FileIO fileIO;
	private ConnectionServiceAndroidClient csClient;
	private Device thisDevice;
	private User user;
	private FileServiceHost fileServiceHost;
	
	public void onCreate(Bundle b){
		super.onCreate(b);
	}
	
	public Commons(){
		fileIO = new FileIO();
	}
	public void startClient(){
		Log.i("Commons", "startClient()");
		csClient = new ConnectionServiceAndroidClient();
	}
//	public void startFileHost(){
//		fileServiceHost = new FileServiceHost();
//	}
	public void run(){
		try {
			fileServiceHost = new FileServiceHost();
			fileServiceHost.startService();
			while(true){
				SSLSocket client = fileServiceHost.receiveMessageConnection();
				fileServiceHost.handleRequest(client, fileIO, csClient, thisDevice);
			}
		} catch (IOException e) {e.printStackTrace();
		}
		
	}
	public void getDeviceInfo(String deviceUUID, String ip){
		Log.i("Commons", "getDeviceInfo()");
		Device device = new Device();
		device.setDeviceType(DeviceType.ANDROID);
		device.setIpAddress(ip);
		device.setUUID(deviceUUID);
		String deviceName = "";
		//
		try{
			InetAddress local = InetAddress.getLocalHost();			
			deviceName = local.getHostName().toString();
		}catch(UnknownHostException e){e.printStackTrace();
		}
		device.setDeviceName(deviceName);
		
		Device host = csClient.getDevice(deviceUUID, false);
		Log.i("Commons", host.toString());
		
		ArrayList<String> config = fileIO.getConfigFile();//
		String pairKey = "";
		for(int i = 0; i < config.size(); i++){
			String s = config.get(i);
			String[] value = s.split(":");
			if(value[0].equals("rsaexp")){
				pairKey = value[1];
			}
		}
		if(!host.getDeviceName().equalsIgnoreCase("NOTFOUND")){
			//Updates IP on start
			if(!host.getIpAddress().equals(device.getIpAddress())){
				host.setIpAddress(device.getIpAddress());
			}
			device = host;
			
			if(!userDeviceExists(device)){
				System.out.println("ADD USER DEVICE");
				csClient.addUserDevice(device.getUUID(), String.valueOf(user.getId()));
			}
			//Updates PairKey
			String encPairKey = Encryption.digest(pairKey);
			csClient.updateDevicePair(device.getUUID(), encPairKey);
			
			
		}else{			
			csClient.makeCall("addDeviceAnd", deviceUUID, deviceName, ip, DeviceType.ANDROID.toString(), null);
			String encPairKey = Encryption.digest(pairKey);
			csClient.insertDevicePair(deviceUUID, encPairKey);
			if(!userDeviceExists(device))
				csClient.addUserDevice(device.getUUID(), String.valueOf(user.getId()));
		}
		
		this.thisDevice = device;
	}
	private boolean userDeviceExists(Device device){
		ArrayList<Device> devices = csClient.getDevicesByUser(String.valueOf(getUser().getId()));
		
		boolean added = false;
		for(Device d : devices){
			if(d.getUUID().equals(device.getUUID())){
				added = true;
				break;
			}
		}
		return added;
	}
	
	//Getters
	public FileIO getFileIO() {
		return fileIO;
	}
	public ConnectionServiceAndroidClient getCsClient() {
		return csClient;
	}
	public Device getThisDevice() {
		return thisDevice;
	}
	public User getUser(){
		return user;
	}
	public FileServiceHost getFileServiceHost(){
		return fileServiceHost;
	}//
	//Setters
	public void setFileIO(FileIO fileIO) {
		this.fileIO = fileIO;
	}
	public void setCsClient(ConnectionServiceAndroidClient csClient) {
		this.csClient = csClient;
	}
	public void setThisDevice(Device thisDevice) {
		this.thisDevice = thisDevice;
	}
	public void setUser(User user){
		this.user = user;
	}
	public void setFileServiceHost(FileServiceHost fileServiceHost){
		this.fileServiceHost = fileServiceHost;
	}

}
